<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 分类管理
 * 
 * @author  : $Author: spike $
 * @date    : $Date: 2014-02-25 13:43:37 +0800 (二, 2014-02-25) $
 * @version : $Id: category.php 31 2014-02-25 05:43:37Z spike $
 * @rev     : $Revision: 31 $
 */
class Category extends CI_Controller 
{  
    public function __construct()
    {
        parent::__construct();

    }

    /**
     * [浏览]
     */
    public function browse()
    {
        $this->load->model('Category_model');
        $list = $this->Category_model->get_category_by_depth(CATEGORY_MAX_DEPTH, FALSE);

        // 统计
        $this->load->dao('Category_dao');
        $total = $this->Category_dao->count(array(
            'is_delete' => UNDELETED
        ));

        $data = array(
            'total'           => $total,
            'list'            => $list,
            'category_status' => $this->config->item('category_status'),
            'css'             => array(JS_DOMAIN.'jquery/jquery-nestable/jquery.nestable.css'),
            'js'              => array('jquery/jquery-nestable/jquery.nestable.js','jquery/jquery-json/jquery.json.js'),
        );

        $this->load->view('home/header.tpl', $data);
        $this->load->view('category/browse.tpl');
        $this->load->view('home/footer.tpl');
    }

    /**
     * [新增]
     */
    public function add()
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            temp('add_category', $params);

            $this->load->model('Category_model');

            if( FALSE === $category_id = $this->Category_model->add_category($params) )
            {
                show_error($this->Category_model->get_error(), 200, '错误', 'error_general_lite');
            }

            temp('add_category', NULL);

            show_message(
                "新增分类成功！", 
                array('编辑分类' => DOMAIN."category/edit/{$category_id}",),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            $this->load->model('Category_model');
            $category = $this->Category_model->get_category_by_depth(CATEGORY_MAX_DEPTH, FALSE);

            $this->load->model('Attr_model');
            $attr_group = $this->Attr_model->browse_attr_group(
                array('status' => ATTR_GROUP_STATUS_ON),
                NULL,
                NULL,
                '`attr_group_id` ASC'
            );

            $page_style    = $this->config->item('page_style');
            $data = array(
                'temp'            => temp('add_category'),
                'category'        => $category,
                'attr_group'      => $attr_group['data'],
                'page_style'      => $page_style['LITE'],
                'category_status' => $this->config->item('category_status'), 
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('category/add.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [编辑]
     */
    public function edit($id = NULL)
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            $this->load->model('Category_model');

            if( FALSE === $this->Category_model->edit_category($id, $params) )
            {
                show_error($this->Category_model->get_error(), 200, '错误', 'error_general_lite');
            }

            show_message(
                "编辑分类成功！", 
                array('查看编辑的分类' => DOMAIN."category/edit/{$id}",),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            $this->load->model('Category_model');
            $category = $this->Category_model->get_category($id, FALSE);

            $this->load->model('Attr_model');
            $attr_group = $this->Attr_model->browse_attr_group(
                array('status' => ATTR_GROUP_STATUS_ON),
                NULL,
                NULL,
                '`attr_group_id` ASC'
            );

            $page_style    = $this->config->item('page_style');
            $data = array(
                'category'     => $category,
                'attr_group'   => $attr_group['data'],
                'page_style'   => $page_style['LITE'],
                'brand_status' => $this->config->item('brand_status'),
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('category/edit.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [批零编辑]
     */
    public function batch_edit()
    {
        $data    = $this->input->get('data');
        $is_ajax = $this->input->get('is_ajax');
        $data    = json_decode($data, TRUE);
        
        $this->load->model('Category_model');
        if( FALSE === $this->Category_model->batch_edit_category($data))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Category_model->get_error());
            }
            else
            {
                show_error($this->Category_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
            else
            {
                show_message("批量编辑分类成功！", array(
                    '返回会员管理' => DOMAIN."category/browse",
                ));
            }
        }
    }


    /**
     * [改变状态]
     */
    public function change_status($id = NULL)
    {
        $act     = $this->input->get('act');
        $is_ajax = $this->input->get('is_ajax');

        $this->load->model('Category_model');
        if( FALSE === $this->Category_model->change_category_status($id, $act))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Category_model->get_error());
            }
            else
            {
                show_error($this->Category_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
            else
            {
                show_message("改变分类状态成功！", array(
                    '返回会员管理' => DOMAIN."category/browse",
                ));
            }
        }
    }

}
/* End of file */